package com.zhongzhi.serviceimpl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhongzhi.dao.MeasureDao;
import com.zhongzhi.service.MeasureService;
import com.zhongzhi.springboot.pojo.Aisle;
import com.zhongzhi.springboot.pojo.Bo;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.JsonResult;
import com.zhongzhi.springboot.pojo.Measure;
import com.zhongzhi.springboot.pojo.Measure_AT;

@Service
public class MeasureServiceImpl implements MeasureService{

	@Autowired
	private MeasureDao measureDao;
	//增
	@Override
	public Bo insert(Measure measure) {
		int a = measureDao.insert(measure);
		if(a==0) {
			return new Bo(false,"插入数据失败",0,null);
		}
		return new Bo(true,"",a,null);
	}

	//改
	@Override
	public Bo update(Measure measure) {
		if (measure.getDeviceNum()==null || measure.getDeviceNum().length()==0) {
			return new Bo(false,"机号不能为空",0,null);
		}else if (measure.getTypId()==null || measure.getTypId()<=0) {
			return new Bo(false,"机型不正确",0,null);
		}else if (measure.getWeight()==null || measure.getWeight()<=0.0) {
			return new Bo(false,"负重量不正确",0,null);
		}else {
			int a = measureDao.update(measure);
			if (a==0) {
				return new Bo(false,"修改数据失败",0,null);
			}
			return new Bo(false,"",a,null);
		}
	}

	//删
	@Override
	public Bo delete(Integer id) {
		if(id<=0) {
			return new Bo(false,"数据选择有误",0,null);
		}
		int b = measureDao.delete(id);
		if(b==0) {
			return new Bo(false,"删除通道数据失败",0,null);
		}
		return new Bo(true,"",b,null);
	}
	//批量删除
	@Override
	public Bo deletes(Integer[] ids) {
		if(ids.length==0) {
			return new Bo(false,"未选择数据",0,null);
		}
		int b = measureDao.deletes(ids);
		if(b==0) {
			return new Bo(false,"删除数据失败",0,null);
		}else if (b<ids.length) {
			return new Bo(false,b+"条数据删除成功",0,null);
		}
		return new Bo(true,"",b,null);
	}
	//查
	@Override
	public Bo select(Integer pages, Integer line) {
		//数据校验
		//页数
		if(pages==null || pages<0) {
			pages = 0;
		}
		//条数
		if(line ==null || line<=0) {
			line=20;
		}
		pages=pages*line;
		List<Measure_AT> list = measureDao.select(pages,line);
		return new Bo(true, "",list.size(),list);
	}
	//查询总数
	@Override
	public Bo selectSize() {
		int a = measureDao.selectSize();
		return new Bo(true, "",a,null);
	}

	//根据id查询
	@Override
	public Bo selectById(Integer id) {
		//数据校验
		if (id<=0) {
			return new Bo(false, "数据id有误",0,null);
		}else {
			Measure_AT measure = measureDao.selectById(id);
			if (measure==null) {
				return new Bo(false, "无此id数据",0,null);
			}else {
				return new Bo(true, "",10,measure);
			}
		}
	}

	//根据机号查询
	@Override
	public Bo selectByDeviceNum(String deviceNum,Integer pages,Integer line) {
		//数据校验
		//页数
		if(pages==null || pages<0) {
			pages = 0;
		}
		//条数
		if(line ==null || line<=0) {
			line=20;
		}
		pages=pages*line;
		List<Measure_AT> list = measureDao.selectByDeviceNum(deviceNum,pages,line);
		return new Bo(true, "",list.size(),list);
	}

	//根据机号查询总数
	@Override
	public Bo selectByDeviceNumSize(String deviceNum) {
		int a = measureDao.selectByDeviceNumSize(deviceNum);
		return new Bo(true, "",a,null);
	}

	//根据机型查询信息
	@Override
	public Bo selectByTypeId(Integer TypeId,Integer pages,Integer line) {
		//数据校验
		//页数
		if(pages==null || pages<0) {
			pages = 0;
		}
		//条数
		if(line ==null || line<=0) {
			line=20;
		}
		pages=pages*line;
		List<Measure_AT> list = measureDao.selectByTypeId(TypeId,pages,line);
		return new Bo(true, "",list.size(),list);
	}
	//根据机型查询总数
	@Override
	public Bo selectByTypeIdSize(Integer TypeId) {
		int a = measureDao.selectByTypeIdSize(TypeId);
		return new Bo(true, "",a,null);
	}

	//时间查询数据
	@Override
	public Bo selectByTDate(Long date1,Long date2,Integer pages,Integer line) {
		//数据校验
		//页数
		if(pages==null || pages<0) {
			pages = 0;
		}
		//条数
		if(line ==null || line<=0) {
			line=20;
		}
		if(date1>date2) {
			return new Bo(true, "时间选择错误",0,null);
		}
		pages=pages*line;
		List<Measure_AT> list = measureDao.selectByTData(date1,date2,pages,line);
		return new Bo(true, "",list.size(),list);
	}
	//时间查询数量
	@Override
	public Bo selectByTDateSize(Long date1,Long date2) {
		int a = measureDao.selectByTDataSize(date1,date2);
		return new Bo(true, "",a,null);
	}



}
